package cn.lastcrdg.testitemdecoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2018/4/10 14:37
 * description：悬浮的装饰器
 */
public class SticklyItemDecoration  extends RecyclerView.ItemDecoration{

    private static final String TAG = SticklyItemDecoration.class.getSimpleName();
    private int dividerHeight = SizeUtils.dp2px(2);
    private int headerHeight = SizeUtils.dp2px(40);

    private GroupInfoCallback mGroupInfoCallback;
    private Paint mPaint;

    public SticklyItemDecoration(Context context, GroupInfoCallback callback) {
        mPaint = new Paint();
        mPaint.setColor(Color.GRAY);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mGroupInfoCallback = callback;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        GroupInfo groupInfo = mGroupInfoCallback.getGroupInfo(position);
        if (groupInfo.isFirstViewInGroup()){
            outRect.top = headerHeight;
        } else {
            outRect.top = dividerHeight;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);

        int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(view);
            GroupInfo groupInfo = mGroupInfoCallback.getGroupInfo(position);
            if (i != 0 && !groupInfo.isFirstViewInGroup()){
                int left = view.getLeft() + view.getPaddingLeft();
                int top = view.getTop() + view.getPaddingTop() - dividerHeight;
                int right = left + view.getMeasuredWidth();
                int bottom = view.getTop() + view.getPaddingTop();

                Rect rect = new Rect(left, top, right, bottom);
                mPaint.setColor(Color.GRAY);
                c.drawRect(rect, mPaint);
            }
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int count = parent.getChildCount();

        for (int i = 0; i < count; i++) {

            View view = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(view);
            GroupInfo groupInfo = mGroupInfoCallback.getGroupInfo(position);

            int left = parent.getPaddingLeft();
            int right = left + parent.getMeasuredWidth() - parent.getPaddingRight();

            if (i == 0){
                //当前是第一个可见的条目
                int top = parent.getPaddingTop();

                if (groupInfo.isLastViewInGroup()){
                    //当前是可见的最后一个条目
                int suggestTop = view.getBottom() - headerHeight;
                if ( suggestTop < top ) {
                    //判断最后一个条目和header两者底端是不是对齐
                    top = suggestTop;
                    }
                }
                int bottom = top + headerHeight;

//                Log.d(TAG, "group-" + (groupInfo.getGroupId() + 1) + "--pos-" + i + "--bottom:" + view.getBottom() + "--height:" + headerHeight );
//                if (groupInfo.isLastViewInGroup()){
//                    Log.d(TAG, "onDrawOver: " + (view.getBottom() == headerHeight));
//                    bottom = view.getBottom();
//                    top = bottom  - headerHeight;
//                }

                mPaint.setColor(Color.GREEN);
                Rect rect = new Rect(left, top, right, bottom);
                c.drawRect(rect, mPaint);
                mPaint.setColor(Color.RED);
                mPaint.setTextSize(SizeUtils.sp2px(16));
                c.drawText(String.valueOf(groupInfo.getGroupId() + 1), left + SizeUtils.dp2px(10),
                        (bottom + top) / 2, mPaint);
            } else {
                //当前不是第一个可见的条目
                if (groupInfo.isFirstViewInGroup()){
                    //如果是组内的第一个条目

                    int top = view.getTop() - headerHeight;
                    int bottom = view.getTop();

                    mPaint.setColor(Color.GREEN);
                    Rect rect = new Rect(left, top, right, bottom);
                    c.drawRect(rect, mPaint);
                    mPaint.setColor(Color.RED);
                    mPaint.setTextSize(SizeUtils.sp2px(16));
                    c.drawText(String.valueOf(groupInfo.getGroupId() + 1), left + SizeUtils.dp2px(10),
                            (bottom + top) / 2, mPaint);
                }
            }
        }
    }
}
